<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>GoodERP</title>
  <link rel="shortcut icon" href="/web/static/src/img/favicon.ico" type="image/x-icon">
  <link rel="stylesheet" href="/web/static/lib/fontawesome/css/font-awesome.css">
  <link rel="stylesheet" href="/web/static/lib/bootstrap/css/bootstrap.css">
  <script src="/web/static/lib/jquery/jquery.js" type="text/javascript"></script>
  <script src="/web/static/lib/bootstrap/js/modal.js"></script>
  <script src="/web/static/lib/bootstrap/js/tooltip.js"></script>
  <script src="/web/static/lib/bootstrap/js/dropdown.js"></script>
  <script type="text/javascript">
  $(function() {
      // Little eye
      $('body').on('mousedown','.o_little_eye',function(ev) {
          $(ev.target).siblings('input').prop('type','text');
      });
      $('body').on('mouseup','.o_little_eye',function(ev) {
          $(ev.target).siblings('input').prop('type','password');
      });
      // db modal
      $('body').on('click','.o_database_action', function(ev) {
          ev.preventDefault();
          var db = $(ev.currentTarget).data('db');
          var target = $(ev.currentTarget).data('target');
          $(target).find('input[name=name]').val(db);
          $(target).modal();
      });
      // close modal on submit
      $('.modal').on('click','input[type="submit"]', function(ev) {
          var modal  = $(this).parentsUntil('body', '.modal');
          if (modal.hasClass('o_database_backup')) {
            $(modal).modal('hide');
            if (!$('.alert-backup-long').length) {
              $('.list-group').before("<div class='alert alert-info alert-backup-long'>备份数据库操作比较耗时</div>");              
            }
          }
      });
  });
  </script>
</head>

{% macro master_input() -%}
  <div class="form-group">
    {% if insecure %}
      <input type="hidden" name="master_pwd" class="form-control" value="admin"/>
    {% else %}
      <label for="master_pwd" class="control-label">超级密码</label>
      <input id="master_pwd" type="password" name="master_pwd" class="form-control" required="required" autofocus="autofocus"/>
    {% endif %}
  </div>
{%- endmacro %}

{% macro create_form() -%}
  <p>GoodERP启动成功！请填写下列信息新建数据库</p>
  {{ master_input() }}
  <div class="form-group">
    <div class="row">
      <div class="col-md-12">
        <label for="name" class="control-label">数据库名（英文）</label>
        <input id="name" type="text" name="name" class="form-control" required="required" autocomplete="off"/>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="row">
      <div class="col-md-12">
        <label for="login" class="control-label">用户名</label>
        <input id="login" type="text" name="login" class="form-control" required="required" autocomplete="off"/>
      </div>
    </div>
  </div>
  <div class="form-group has-feedback">
    <label for="password" class="control-label">密码</label>
    <input id="password" type="password" name="password" class="form-control" required="required" autocomplete="off"/>
    <span class="fa fa-eye o_little_eye form-control-feedback" aria-hidden="true" style="cursor: pointer; pointer-events: auto"></span>
  </div>
  <div class="form-group" style="display:none;">
    <div class="row">
      <div class="col-md-6">
        <label for="lang" class="control-label">语言</label>
        <select id="lang" name="lang" class="form-control" required="required" autocomplete="off">
          {% for lang in langs %}
              <option {% if lang[0] == "zh_CN" %}selected="selected" {% endif %}value="{{ lang[0] }}">{{ lang[1] }}</option>
          {% endfor %}
        </select>
      </div>
      <div class="col-md-6">
          <label for="country" class="control-label">国家</label>
          <select id="country" name="country_code" class="form-control" autocomplete="off">
              <option value=""></option>
            {% for country in countries %}
              <option {% if country[0] == "cn" %}selected="selected" {% endif %}value="{{ country[0] }}">{{ country[1] }}</option>
            {% endfor %}
            </select>
        </div>
    </div>
  </div>
  <div class="form-group" style="display:none;">
    <div class="checkbox">
      <label>
        <input name="demo" type="checkbox" class="pull-right" value="1">
        <span>加载自动化测试数据</span>
        <span class="text-muted"> (仅用于演示GoodERP标准功能)</span>
      </label>
    </div>
  </div>
{%- endmacro %}

<body class="container" style="background:url('/core/static/src/img/back.jpg');">
  <!-- Database List -->
  <div class="row">
    <div class="col-md-6 col-md-offset-3 o_database_list">
      <p/>
      <div class="text-center">
        <img src="/web/static/src/img/logo.png" class="img-responsive center-block"/>
      </div>
      <p/>
      {% if insecure and databases %}
      <div class="alert alert-warning">
      请注意，尚未设置超级密码（默认值为admin）。
      请点击<a href="#" data-toggle="modal" data-target=".o_database_master">设置超级密码</a>
      避免数据丢失。
      </div>
      {% endif %}
      {% if error %}
      <div class="alert alert-danger">{{ error }}</div>
      {% endif %}
      {% if databases %}
        <div class="list-group">
          {% for db in databases %}
          <a href="/web?db={{ db }}" class="list-group-item">
            {{ db }}
            {% if manage %}
            <div class="text-right pull-right">
            <span data-db="{{ db }}" data-target=".o_database_backup" class="o_database_action btn-link"><i class="fa fa-floppy-o fa-fw"></i> 备份</span>
            <span data-db="{{ db }}" data-target=".o_database_duplicate" class="o_database_action btn-link"><i class="fa fa-files-o fa-fw"></i> 复制</span>
            <span data-db="{{ db }}" data-target=".o_database_delete" class="o_database_action btn-link"><i class="fa fa-trash-o fa-fw"></i> 删除</span>
            </div>
            {% endif %}
          </a>
          {% endfor %}
        </div>
        {% if manage %}
        <div class="text-left">
          <button type="button" data-toggle="modal" data-target=".o_database_create" class="btn btn-sm btn-primary">
            创建数据库
          </button>
          <button type="button" data-toggle="modal" data-target=".o_database_restore" class="btn btn-sm btn-primary">
            恢复数据库
          </button>
          <button type="button" data-toggle="modal" data-target=".o_database_master" class="btn btn-sm btn-primary">
            设置超级密码
          </button>
        </div>
        {% else %}
        <div class="text-center">
          <a href="/web/database/manager">管理数据库</a>
        </div>
        {% endif %}
      {% else %}
        <form role="form" action="/web/database/create" method="post">
          {{ create_form() }}
          <input type="submit" value="创建数据库" class="btn btn-primary pull-left"/>
        </form>
        <a role="button" data-toggle="modal" data-target=".o_database_restore" class="btn btn-link">
          或恢复已有数据库
        </a>
      {% endif %}
    </div>
  </div>

  <!-- Create -->
  <div class="modal fade o_database_create" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <form role="form" action="/web/database/create" method="post">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">创建数据库</h4>
          </div>
          <div class="modal-body">
            {{ create_form() }}
          </div>
          <div class="modal-footer">
            <input type="submit" value="继续" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Restore -->
  <div class="modal fade o_database_restore" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">恢复数据库</h4>
        </div>
        <form id="form_restore_db" role="form" action="/web/database/restore" method="post" enctype="multipart/form-data">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="backup_file" class="control-label">数据库备份文件</label>
              <input id="backup_file" type="file" name="backup_file" class="required"/>
            </div>
            <div class="form-group">
              <label for="name" class="control-label">数据库名</label>
              <input id="name" type="text" name="name" class="form-control" required="required"/>
            </div>
            <div class="form-group">
                <label for="copy">数据库可能是从其他服务器移入的也可能是本服务器复制出来的。</label>
                <p class="help-block">为了避免数据库之间发生冲突，我们需要知道本次恢复的数据库是移入的还是复制的。
                如果您不确定，请选择 "这是个复制的数据库"。</p>
              <div class="radio">
                <label>
                  <input name="copy" type="radio" class="pull-right" value="true" checked="1">
                  这是个复制的数据库
                </label>
              </div>
              <div class="radio">
                <label>
                  <input name="copy" type="radio" class="pull-right" value="false">
                  这是个从其他服务器移入的数据库
                </label>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <input type="submit" value="继续" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>


  <!-- Master password -->
  <div class="modal fade o_database_master" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">设置超级密码</h4>
        </div>
        <form id="form_change_pwd" role="form" action="/web/database/change_password" method="post">
          <div class="modal-body">
            <p>在对数据库做创建、删除、备份和恢复时要求输入这个密码</p>
            {{ master_input() }}
            <div class="form-group has-feedback">
              <label for="master_pwd_new" class="control-label">新的数据库管理员密码</label>
              <input id="master_pwd_new" type="password" name="master_pwd_new" class="form-control" required="required" autocomplete="off"/>
              <span class="fa fa-eye o_little_eye form-control-feedback" aria-hidden="true" style="cursor: pointer; pointer-events: auto"></span>
            </div>
          </div>
          <div class="modal-footer">
            <input type="submit" value="继续" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>


  <!-- Duplicate DB -->
  <div class="modal fade o_database_duplicate" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">复制数据库</h4>
        </div>
        <form id="form-duplicate-db" role="form" action="/web/database/duplicate" method="post">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="name" class="control-label">数据库名</label>
              <input id="name" type="text" name="name" class="form-control" required="required" readonly="readonly"/>
            </div>
            <div class="form-group">
              <label for="new_name" class="control-label">新数据库名</label>
              <input id="new_name" type="text" name="new_name" class="form-control" required="required"/>
            </div>
          </div>
          <div class="modal-footer">
          <input type="submit" value="继续" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Drop DB -->
  <div class="modal fade o_database_delete" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">删除数据库</h4>
        </div>
        <form id="form_drop_db" role="form" action="/web/database/drop" method="post">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="name" class="control-label">数据库</label>
              <input id="name" type="text" name="name" class="form-control" required="required" readonly="readonly"/>
            </div>
          </div>
          <div class="modal-footer">
            <input type="submit" value="删除" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Backup DB -->
  <div class="modal fade o_database_backup" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">备份数据库</h4>
        </div>
        <form id="form_backup_db" role="form" action="/web/database/backup" method="post">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="name" class="control-label">数据库名</label>
              <input id="name" type="text" name="name" class="form-control" required="required" readonly="readonly"/>
            </div>
            <div class="form-group">
              <label for="backup_format" class="control-label">备份文件格式</label>
              <select id="backup_format" name="backup_format" id="backup_format" class="form-control" required="required">
                <option value="zip">zip (包含附件文件)</option>
                <option value="dump">pg_dump 自定义格式 (不含附件文件)</option>
              </select>
            </div>
          </div>
          <div class="modal-footer">
          <input type="submit" value="备份" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

</body>
</html>
